﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using BllFactory;
using Common;
using IBll;
using Model.Database;
using Model.Sys;
using Model.Sys.Config;
using ViewModel;

namespace Controllers.Filter
{
    public class PowerAttribute:FilterAttribute, IActionFilter,IDependency
    {
        public IBllSession CurrentBllSession { get; set; }

        /// <summary>
        /// 权限Id
        /// </summary>
        public string PowerId { get; set; }
        /// <summary>
        /// 登录的用户信息
        /// </summary>
        protected UserInfo loginUser = null;

        public PowerAttribute()
        {
            CurrentBllSession=IocHelper.AutofacResolve<IBllSession>();
        }


        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool isPass = false;

            Cache<UserInfo> cache=CurrentBllSession.CacheBll.GetCache<UserInfo>(CookieHelper.GetCookieValue(CookieConfig.UserInfoLoginCookieName));
            if (cache != null)
            {
                this.loginUser = (UserInfo)cache.Value;
            }
            if (loginUser != null)
            {
                if (loginUser.IsSuper.HasValue&&loginUser.IsSuper.Value)//超级管理员
                {
                    isPass = true;
                }
                else
                {
                    List<Power> powerList = new List<Power>();

                    foreach (var role in loginUser.Roles)
                    {
                        powerList.AddRange(role.Powers.ToList()); ;
                    }
                    foreach (var department in loginUser.Departments)
                    {
                        foreach (var role in department.Roles)
                        {
                            powerList.AddRange(role.Powers.ToList()); ;
                        }
                    }
                    HashSet<Power> powerSet = new HashSet<Power>(powerList);
                    powerList.Clear();
                    powerList = powerSet.ToList();
                    if (powerList.Any(a => a.PowerId == PowerId))//用户拥有权限
                    {
                        isPass = true;
                    }
                }
            }
            if (!isPass)//无权访问，则跳转
            {
                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.Result = new JsonResult()
                    {
                        ContentEncoding = Encoding.UTF8,
                        Data = new ResultModel
                        {
                            Pass = false,
                            Msg = "无权访问"
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                else
                {
                    filterContext.Result = new ContentResult() { Content = "<script>alert('无权访问');</script>" };
                }
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            //throw new NotImplementedException();
        }
    }
}